import Vue from 'vue'
import Vuex from 'vuex';
import moduleA from './modelA.js';
import moduleB from './modelB.js';

Vue.use( Vuex )


// vuex 3.x 中得语法：
const store = new Vuex.Store( {
  modules: {
    a: moduleA,
    b: moduleB,
  },
  state: {
    count: 0,
    token: sessionStorage.getItem( 'token' ) ? sessionStorage.getItem( 'token' ) : '',
  },
  getters: {
    getStateCount ( state ) {
      return state.count;
    },
    getToken ( state ) {
      return state.token;
    },
    getCount ( state ) {
      return state.count;
    }
  },
  actions: {
    countAc ( { commit }, payload ) {
      commit( 'countMt', payload )
    },
    tokenAc ( { commit }, payload ) {
      commit( 'tokenMt', payload )

    }
  },
  mutations: {
    tokenMt ( state, payload ) {
      state.token = payload
    },
    countMt ( state, payload ) {
      state.count = payload.number;  // dispatch('countAc',{nubmer:10});dispatch({type:'countAc',nubmer:10})
    }
  }
} )

// 使用es6 export default 公开store;
export default store;